<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\pay\controller;

use cmf\controller\AdminBaseController;
use think\db\Query;
use think\facade\Db;

class VipController extends AdminBaseController
{

    /**
     * Notes:vip列表
     * User:
     * DateTime: 2022-01-17 14:57
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $param = $this->request->param();
        $list = Db::name("member_level")
            ->where(function (Query $query) use ($param) {
                $query->where(['delete_time' => 0]);
            })
            ->order("id desc")
            ->paginate(30);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        return $this->fetch();
    }

    /**
     * Notes:添加vip页面
     * User:
     * DateTime: 2022-01-17 16:53
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * Notes:执行添加vip
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function addPost()
    {
        $param = $this->request->param();
        $data['name'] = $param['name'];
        $data['rate'] = $param['rate'];
        $data['integral'] = $param['integral'];
        $data['wheels'] = $param['wheels'];
        $data['create_time'] = date("Y-m-d H:i:s");
        $res = Db::name("member_level")->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:编辑vip页面
     * User:
     * DateTime: 2022-01-17 17:11
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $param = $this->request->param();
        $info = Db::name("member_level")->where(["id" => $param['id']])->find();
        $this->assign("info", $info);
        return $this->fetch();
    }

    /**
     * Notes:执行编辑vip
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function editPost()
    {
        $param = $this->request->param();
        $data['name'] = $param['name'];
        $data['rate'] = $param['rate'];
        $data['integral'] = $param['integral'];
        $data['wheels'] = $param['wheels'];
        $data['update_time'] = time();
        $res = Db::name("member_level")->where(["id" => $param['id']])->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:删除
     * User:
     * DateTime: 2022-01-17 15:10
     * @throws \think\Exception
     */
    public function delete()
    {
        $this->deleteInfo("member_level");
        $this->success("删除成功");
    }

}
